//
//  Solution.swift
//  203-移除链表元素
//
//  Created by lhc on 2023/9/14.
//

import Foundation

class Solution {
    static func removeElements(_ head: ListNode<Int>?, _ val: Int) -> ListNode<Int>? {
        
        let dummyNode = ListNode(val: 0)
        dummyNode.next = head
        var pre = dummyNode
        
        while(pre.next != nil) {
            if pre.next!.val == val {
                pre.next = pre.next!.next
            }else {
                pre = pre.next!
            }
        }
        
        return dummyNode.next
    }
    
    
    
    static func removeElementsByRecurison(_ head: ListNode<Int>?, _ val: Int) -> ListNode<Int>? {
        
        var cur = head
        if cur == nil {
            return nil
        }
        
        if cur!.val == val {
            cur = removeElementsByRecurison(cur!.next, val)
        }else {
            cur!.next = removeElementsByRecurison(cur!.next, val)
        }
        
        
        return cur
    }
}

